package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;
import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("factorAction")
public class FactorAction extends MyAction {
	private static final long serialVersionUID = 1L;
	private FactorService service;
	private GroupService groupService;

	public FactorService getService() {
		return service;
	}

	@Resource
	public void setService(FactorService service) {
		this.service = service;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	@Resource
	public void setGroupServices(GroupService groupService) {
		this.groupService = groupService;
	}

	public String list() throws IOException {
		GL.info("user.list()函数  ");
		beginReqParam();
		// 获取表达参数

		List<T_factor> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_factor to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{fac_id:'" + to.getFac_id() + "',fac_name:'" + to.getFac_name() + "',fac_unit:'"
					+ to.getFac_unit() + "',fac_alarm_lower1:'" + to.getFac_alarm_lower1() + "',fac_alarm_upper1:'"
					+ to.getFac_alarm_upper1() + "',fac_alarm_lower2:'" + to.getFac_alarm_lower2()
					+ "',fac_alarm_upper2:'" + to.getFac_alarm_upper2() + "',fac_range_lower:'"
					+ to.getFac_range_lower() + "',fac_range_upper:'" + to.getFac_range_upper() + "',fac_dev_lower:'"
					+ to.getFac_dev_lower() + "',fac_dev_upper:'" + to.getFac_dev_upper() + "',fac_memo:'"
					+ to.getFac_memo() + "'}";
			jsonString = jsonString + t;
		}

		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();

		JSONArray array = JSONArray.fromObject(service.queryAll());
		out.write(array.toString());
		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		T_factor to = service.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_fac_name:'" + to.getFac_name() + "',f_fac_unit:'" + to.getFac_unit()
				+ "',f_fac_alarm_lower1:'" + to.getFac_alarm_lower1() + "',f_fac_alarm_upper1:'"
				+ to.getFac_alarm_upper1() + "',f_fac_alarm_lower2:'" + to.getFac_alarm_lower2()
				+ "',f_fac_alarm_upper2:'" + to.getFac_alarm_upper2() + "',f_fac_range_lower:'"
				+ to.getFac_range_lower() + "',f_fac_range_upper:'" + to.getFac_range_upper() + "',f_fac_dev_lower:'"
				+ to.getFac_dev_lower() + "',f_fac_dev_upper:'" + to.getFac_dev_upper() + "',f_fac_memo:'"
				+ to.getFac_memo() + "'}}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_fac_name = request.getParameter("f_fac_name");
		String f_fac_unit = request.getParameter("f_fac_unit");

		String f_fac_alarm_lower1 = request.getParameter("f_fac_alarm_lower1");
		String f_fac_alarm_upper1 = request.getParameter("f_fac_alarm_upper1");
		String f_fac_alarm_lower2 = request.getParameter("f_fac_alarm_lower2");
		String f_fac_alarm_upper2 = request.getParameter("f_fac_alarm_upper2");
		String f_fac_range_lower = request.getParameter("f_fac_range_lower");
		String f_fac_range_upper = request.getParameter("f_fac_range_upper");
		String f_fac_dev_lower = request.getParameter("f_fac_dev_lower");
		String f_fac_dev_upper = request.getParameter("f_fac_dev_upper");
		String f_fac_memo = request.getParameter("f_fac_memo");

		T_factor to = new T_factor();

		to.setFac_name(f_fac_name);
		to.setFac_unit(f_fac_unit);

		to.setFac_alarm_lower1(Double.parseDouble(f_fac_alarm_lower1));
		to.setFac_alarm_lower2(Double.parseDouble(f_fac_alarm_lower2));
		to.setFac_alarm_upper1(Double.parseDouble(f_fac_alarm_upper1));
		to.setFac_alarm_upper2(Double.parseDouble(f_fac_alarm_upper2));
		to.setFac_range_lower(Double.parseDouble(f_fac_range_lower));
		to.setFac_range_upper(Double.parseDouble(f_fac_range_upper));
		to.setFac_dev_lower(Double.parseDouble(f_fac_dev_lower));
		to.setFac_dev_upper(Double.parseDouble(f_fac_dev_upper));
		to.setFac_memo(f_fac_memo);

		service.add(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("fac_id");
			service.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();

		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		String f_fac_name = request.getParameter("f_fac_name");
		String f_fac_unit = request.getParameter("f_fac_unit");

		String f_fac_alarm_lower1 = request.getParameter("f_fac_alarm_lower1");
		String f_fac_alarm_upper1 = request.getParameter("f_fac_alarm_upper1");
		String f_fac_alarm_lower2 = request.getParameter("f_fac_alarm_lower2");
		String f_fac_alarm_upper2 = request.getParameter("f_fac_alarm_upper2");
		String f_fac_range_lower = request.getParameter("f_fac_range_lower");
		String f_fac_range_upper = request.getParameter("f_fac_range_upper");
		String f_fac_dev_lower = request.getParameter("f_fac_dev_lower");
		String f_fac_dev_upper = request.getParameter("f_fac_dev_upper");
		String f_fac_memo = request.getParameter("f_fac_memo");

		T_factor to = service.query(Integer.parseInt(p_id));

		to.setFac_name(f_fac_name);
		to.setFac_unit(f_fac_unit);

		to.setFac_alarm_lower1(Double.parseDouble(f_fac_alarm_lower1));
		to.setFac_alarm_lower2(Double.parseDouble(f_fac_alarm_lower2));
		to.setFac_alarm_upper1(Double.parseDouble(f_fac_alarm_upper1));
		to.setFac_alarm_upper2(Double.parseDouble(f_fac_alarm_upper2));
		to.setFac_range_lower(Double.parseDouble(f_fac_range_lower));
		to.setFac_range_upper(Double.parseDouble(f_fac_range_upper));
		to.setFac_dev_lower(Double.parseDouble(f_fac_dev_lower));
		to.setFac_dev_upper(Double.parseDouble(f_fac_dev_upper));
		to.setFac_memo(f_fac_memo);

		service.update(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
